package com.kl.airticketserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.airticketserver.dto.Result;
import com.kl.airticketserver.entity.SysRole;
import com.kl.airticketserver.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import java.util.List;

@RestController
@RequestMapping("/api/system/role")
public class SysRoleController {
    
    @Autowired
    private SysRoleService roleService;
    
    @GetMapping("/list")
    public Result<IPage<SysRole>> list(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size,
                                       @RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer status) {
        Page<SysRole> pageParam = new Page<>(page, size);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        
        IPage<SysRole> result = roleService.page(pageParam, wrapper);
        return Result.success(result);
    }
    
    @PostMapping
    public Result<Void> save(@RequestBody SysRole role) {
        roleService.save(role);
        return Result.success(null);
    }
    
    @PutMapping
    public Result<Void> update(@RequestBody SysRole role) {
        roleService.updateById(role);
        return Result.success(null);
    }
    
    @DeleteMapping
    public Result<Void> delete(@RequestBody List<String> ids) {
        roleService.removeByIds(ids);
        return Result.success(null);
    }
}